package graphics.server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Isaac
 *
 * This class take care of the visual.
 */

public class Boom extends JPanel
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public Icon iconAN,iconAW,iconAE,iconANRed,iconAWRed,iconAERed,icon6,
	icon7,iconv,iconb,iconflag,iconice,iconb_2,iconboomEror;
	public Icon[] imageDirections;
	public String pict[] ={"images/AgentNorth.jpg","images/AgentSouth.jpg","images/AgentWest.jpg","images/AgentEast.jpg","images/AgentNorthRed.jpg",
			"images/AgentSouthRed.jpg","images/AgentWestRed.jpg","images/AgentEastRed.jpg","images/view.jpg","images/boom.jpg","images/flag.jpg","images/ice.jpg",
			"images/GhostNorth.jpg","images/GhostWest.jpg","images/GhostSouth.jpg","images/GhostEast.jpg"}, dirPict[]={"images/North.jpg","images/South.jpg","images/East.jpg","images/West.jpg","images/Rest.jpg"};
	public JPanel p;
	public JLabel[] b;
	public JPanel[] bb;
	public JPanel pp;

	//************************************************************************************

	/**
	 * Create a board and initialize it.
	 */
	public void init(int Xsize,int Ysize)
	{
		synchronized(monitor){
			setLayout(new GridLayout());
			iconv = getImage(pict[8]);
			iconb = getImage(pict[9]);
			iconflag = getImage(pict[10]);
			iconice = getImage(pict[11]);
			imageDirections = new Icon[5];
			imageDirections[0] = getImage(dirPict[0]);
			imageDirections[1] = getImage(dirPict[1]);
			imageDirections[2] = getImage(dirPict[2]);
			imageDirections[3] = getImage(dirPict[3]);
			imageDirections[4] = getImage(dirPict[4]);
			pp = new JPanel(new BorderLayout());
			p = new JPanel();
			p.setLayout(new GridLayout(Ysize,Xsize));
			b = new JLabel[Ysize*Xsize];
			bb = new JPanel[Ysize*Xsize];
			for(int j=0;j<(Ysize*Xsize);j++)
			{
				b[j] = new JLabel(iconv);
				b[j].setBorder(BorderFactory.createLineBorder(Color.black));
				bb[j] = new JPanel(new GridLayout(1,1));
				bb[j].add(b[j]);
				bb[j].setToolTipText(j+"");
				p.add(bb[j]);
			}

			for(int j=0;j<Xsize;j++)
				b[j].setIcon(iconb);

			for(int j=Xsize;j<((Ysize*Xsize)-Xsize);j+=Xsize){
				b[j].setIcon(iconb);
				b[j+Xsize-1].setIcon(iconb);
			}
			for(int j=((Ysize*Xsize)-Xsize);j<(Ysize*Xsize);j++){
				b[j].setIcon(iconb);
			}
			//	JPanel header = new JPanel(new GridLayout(1,3));
			//	pp.add(header, BorderLayout.NORTH);
			//	pp.add(p, BorderLayout.CENTER);
			//	add(pp);
			add(p);
		}
	}
	//***********************************************
	/* Sets a Wall */
	public void setWall(int cell){
		b[cell].setIcon(iconb);
	}
	//*************************************************
	/* Sets Flags */
	public void setFlag(int cell){
		b[cell].setIcon(iconflag);
	}
	/* Sets Ice */
	public void setIce(int cell){
		synchronized(monitor){
			bb[cell].removeAll();
			bb[cell].add(b[cell]);
			b[cell].setIcon(iconice);
		}
	}
	/* delete the content of a cell */
	public void deleteContent(int cell){
		synchronized(monitor){
			bb[cell].removeAll();
			bb[cell].add(b[cell]);
			b[cell].setIcon(iconv);
		}
	}
	
	public void setNA(int cell) {
		bb[cell].removeAll();
		bb[cell].add(new JLabel("NA"));
	}
	
	public void setMarkovAction(final int cell, final double utility, final int direction){
		synchronized (monitor) {
			bb[cell].removeAll();
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					bb[cell].setLayout(new GridLayout(1,1));
					JPanel tmp = new JPanel(new GridLayout(2,1));
					String tmp2str = ("  "+ utility);
					if (tmp2str.length()>=9){
						tmp2str = tmp2str.substring(0, 9);
					}
					JLabel tmp2 = new JLabel(tmp2str);
					Font f = tmp2.getFont().deriveFont((float)10);
					tmp2.setFont(f);
					tmp.add(new JLabel(imageDirections[direction]));
					tmp.add(tmp2,1);
					//bb[cell].add(new JLabel(iconv));
					//JLabel t = new JLabel("HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
					bb[cell].add(tmp);
					SwingUtilities.invokeLater(new Runnable(){
						public void run(){
							bb[cell].repaint();
							repaint();
						}
					});
					
				}
			});	
		}
	}
	
	public static String monitor = "";
	/* set the probability for a cell */
	public void setProb(final int cell, final double iceProb, final double s1Prob, final double s2Prob) {
		synchronized(monitor){
			bb[cell].removeAll();
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					bb[cell].setLayout(new GridLayout(1,1));
					JPanel tmp = new JPanel(new GridLayout(3,1));
					String tmp1str = ("  "+ iceProb);
					String tmp2str = ("  "+ s1Prob);
					String tmp3str = ("  "+ s2Prob);
					if (tmp1str.length()>=9){
						tmp1str = tmp1str.substring(0, 9);
					}
					if (tmp2str.length()>=9){
						tmp2str = tmp2str.substring(0, 9);
					}
					if (tmp3str.length()>=9){
						tmp3str = tmp3str.substring(0, 9);
					}
					JLabel tmp1 = new JLabel(tmp1str);
					JLabel tmp2 = new JLabel(tmp2str);
					JLabel tmp3 = new JLabel(tmp3str);
					Font f = tmp1.getFont().deriveFont((float)10);
					tmp1.setFont(f);
					tmp2.setFont(f);
					tmp3.setFont(f);
					tmp.add(tmp1,0);
					tmp.add(tmp2,1);
					tmp.add(tmp3,2);
					//bb[cell].add(new JLabel(iconv));
					//JLabel t = new JLabel("HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
					bb[cell].add(tmp);
					SwingUtilities.invokeLater(new Runnable(){
						public void run(){
							bb[cell].repaint();
							repaint();
						}
					});
					
				}
			});			

		}
	}

	//********************************************************************
	/* Initialize the agent location  */
	public void setAgent(SingleAgent agent) {
		int offset =findIconOffset((String)(agent.getColor()),(String)(agent.getOrientation()));
		b[agent.getLocation()].setIcon(getImage(pict[offset]));
	}

	private int findIconOffset (String color,String orientation){
		int offset = 0;
		if(color.compareToIgnoreCase("Red") == 0)
			offset = 4;
		if (orientation.compareToIgnoreCase("South")==0)
			offset += 1;
		else if (orientation.compareToIgnoreCase("West")==0)
			offset += 2;
		else if (orientation.compareToIgnoreCase("East")==0)
			offset += 3;
		return offset;
	}

	private ImageIcon getImage(String path){
		URL url = getClass().getResource("/" + path);
		if (url == null){
			return new ImageIcon(path);
		} else {
			try {
				return new ImageIcon(ImageIO.read(url));
			} catch (IOException e) {
				return new ImageIcon(path);
			}
		}
	}
}

